﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using LINQSamples.Data;

namespace Web.Samples
{
    public partial class SimpleOrderBy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.BindQuery();
            this.BindImperative();
        }

        private void BindQuery()
        {
            var query = from p in Product.GetProducts()
                        orderby p.Name
                        select p;

            this.litQueryResult.Text = "<strong>Ascending Order</strong><br />\n";
            foreach (Product p in query.Take(10))
                this.litQueryResult.Text += p.Name + "<br />\n";

            this.litQueryResult.Text += "<br />\n";

            query = from p in Product.GetProducts()
                    orderby p.Name descending
                    select p;

            this.litQueryResult.Text += "<strong>Descending Order</strong><br />\n";
            foreach (Product p in query.Take(10))
                this.litQueryResult.Text += p.Name + "<br />\n";
        }

        private void BindImperative()
        {
            var query = Product.GetProducts().OrderBy(p => p.Name).Take(10);
            this.litImperativeResults.Text = "<strong>Ascending Order</strong><br />\n";
            foreach (Product p in query)
                this.litImperativeResults.Text += p.Name + "<br />\n";

            this.litImperativeResults.Text += "<br />\n";

            query = Product.GetProducts().OrderByDescending(p => p.Name).Take(10);
            this.litImperativeResults.Text += "<strong>Descending Order</strong><br />\n";
            foreach (Product p in query)
                this.litImperativeResults.Text += p.Name + "<br />\n";
        }
    }
}
